package ystp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ystp.model.Ysuser;
import ystp.service.YsrenyuanfenpeiService;
import ystp.service.YsuserService;
import ystp.service.YuecaikangjunyaowuService;
import ystp.service.YuecaikangjunyaowujieguoService;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("yuecaikangjunyaowu")
public class YuecaikangjunyaowuController {
    @Resource
    YuecaikangjunyaowuService ddService;

    @Resource
    YuecaikangjunyaowujieguoService ddKangjunyaowujieguoService;

    @Resource
    YsrenyuanfenpeiService ddYsrenyuanfenpeiService;

    @Resource
    YsuserService ddYsuserService;

    public Logger log = LoggerFactory.getLogger(YuecaikangjunyaowuController.class);


    // 查列表
    @RequestMapping("findyuecaikangjunyaowulist")
    public JsonResult<?> findYuecaiKangjunyaowulist(@RequestParam("xiangmuid") String ppXiangmuId, @RequestParam("pageindex") int ppPageIndex,
                                               @RequestParam("pagesize") int ppPageSize){
        try {

            List<Map<String,Object>> mmList = ddService.findYuecaiPagingByXiangmuId(ppXiangmuId,ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getYuecaiCount(ppXiangmuId);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("YuecaikangjunyaowuList", mmList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("yuecaikangjunyaowu/findyuecaikangjunyaowulist:error",e);
            return JsonResult.getErrorResult("yuecaikangjunyaowu/findyuecaikangjunyaowulist:error " + e.getMessage());
        }

    }

    // 查投票列表
    @RequestMapping("findbiaojuelist")
    public JsonResult<?> findBiaojueList(@RequestParam("xiangmuid") String ppXiangmuId,@RequestParam("userid") String ppUserId, @RequestParam("pageindex") int ppPageIndex,
                                                    @RequestParam("pagesize") int ppPageSize){
        try {

            List<Map<String,Object>> mmList = ddService.findYuecaiBiaojueByXiangmuId(ppXiangmuId,ppUserId,ppPageIndex,ppPageSize);
            Integer mmCount = ddService.countYuecaiBiaojueByXiangmuId(ppXiangmuId,ppUserId);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("Biaojuelist", mmList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("yuecaikangjunyaowu/findbiaojuelist:error",e);
            return JsonResult.getErrorResult("yuecaikangjunyaowu/findbiaojuelist:error " + e.getMessage());
        }

    }

    @RequestMapping("getjindulist")
    public JsonResult<?> getjindulist(@RequestParam("xiangmuid") String ppxiangmuId){
        try {
            List<Map<String,Object>> mmJindulist = new ArrayList<>();

            Integer mmTotal=ddService.getYuecaiCount(ppxiangmuId);
            List<Map<String, Object>> mmUserList=ddYsrenyuanfenpeiService.findRenyuanFenpeiByXiangmuId(ppxiangmuId);
            for(Map<String, Object> ysrenyuanfenpei:mmUserList){
                Map<String, Object> mmMap = new HashMap<String, Object>();
                String mmUserId=ysrenyuanfenpei.get("userid").toString();
                Ysuser mmYsuser=ddYsuserService.findOne(mmUserId);
                String mmUserName=mmYsuser.getUsername();

                Integer mmTongyishu= ddKangjunyaowujieguoService.getYuecaikangjunResultCount(ppxiangmuId,mmUserId,"1");

                String mmShifoutijiao="";
                if("1".equals(ysrenyuanfenpei.get("issubmit").toString())){
                    mmShifoutijiao="是";
                }else {
                    mmShifoutijiao="否";
                }
                mmMap.put("username", mmUserName);
                mmMap.put("zongshu", mmTotal);
                mmMap.put("tongyishu", mmTongyishu);
                mmMap.put("shifoutijiao", mmShifoutijiao);
                mmJindulist.add(mmMap);
            }

            return JsonResult.getSuccessResult(mmJindulist);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("yuecaikangjunyaowu/getjindulist:error",e);
            return JsonResult.getErrorResult("yuecaikangjunyaowu/getjindulist:error " + e.getMessage());
        }
    }

    // 刪除
    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xiangmuid") String ppXiangmuId){
        try {

            return ddService.deleteByXiangmuId(ppXiangmuId)==true? JsonResult.getSuccessResult("删除材料成功")
                    : JsonResult.getErrorResult("删除材料失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("yuecaikangjunyaowu/delete:error",e);
            return JsonResult.getErrorResult("yuecaikangjunyaowu/delete:error " + e.getMessage());
        }
    }
}
